package com.chzu.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chzu.mapper.SysMenuMapper;
import com.chzu.mapper.SysPermissionMapper;
import com.chzu.mapper.SysRoleMapper;
import com.chzu.pojo.SysMenu;
import com.chzu.pojo.SysRole;
import com.chzu.result.JsonResult;
import com.chzu.result.PageResult;
import com.chzu.result.QueryInfo;
import com.chzu.service.SysRoleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author lz
 * @Version 1.0
 * @Company 滁州大健康与养老产业研究院
 * @ClassName SysRoleServiceImpl
 * @Description TODO
 * @Date 2022/01/27 15:01
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Resource
    private SysRoleMapper roleMapper;

    @Resource
    private SysMenuMapper menuMapper;

    @Resource
    private SysPermissionMapper permissionMapper;

    /**
     *  分页插叙你
     * @param queryInfo 页码、大小、查询内容
     * @return
     */
    @Override
    public JsonResult<Object> findRole(QueryInfo queryInfo) {
        PageHelper.startPage(queryInfo.getPageNumber(), queryInfo.getPageSize());
        Page<SysRole> page = roleMapper.findRole(queryInfo.getQueryString());
        long total = page.getTotal();
        List<SysRole> result = page.getResult();
        result.forEach(item -> {
            // 查询角色下的菜单信息
            List<SysMenu> roleMenu = menuMapper.findByRoleId(item.getId());
            roleMenu.forEach(menu -> {
                List<SysMenu> children = menuMapper.findByRoleIdAndParentId(menu.getId(), item.getId());
                menu.setChildren(children);
            });
            item.setMenus(roleMenu);
            // 查询该角色权限信息
            item.setPermissions(permissionMapper.findByRoleId(item.getId()));
        });
        return PageResult.pageResult(result, total);
    }

    /**
     *  添加菜单信息
     * @param role 菜单信息
     * @return
     */
    @Transactional
    @Override
    public JsonResult<Object> insertRole(SysRole role) {
        SysRole sr = roleMapper.findByLabel(role.getLabel());
        if (sr != null) {
            return JsonResult.fail("该角色已经存在！");
        }
        roleMapper.insertRole(role);
        if (role.getPermissions().size() > 0) {
            permissionMapper.deleteByRoleId(role.getId());
            role.getPermissions().forEach(item -> {
                roleMapper.insertPermission(role.getId(),item.getId());
            });
        }
        if (role.getMenus().size() > 0) {
            menuMapper.deleteByRoleId(role.getId());
            role.getMenus().forEach(item -> roleMapper.insertMenus(role.getId(),item.getId()));
        }

        return JsonResult.ok("添加菜单信息成功");
    }

    /**
     *
     * @param id
     * @return 菜单id
     */
    @Override
    public JsonResult<Object> deleteRole(Integer id) {
        List<SysMenu> menus = menuMapper.findByRoleId(id);
        List<SysMenu> childrens = new ArrayList<>();
        menus.forEach(item -> {
            childrens.addAll(menuMapper.findByRoleIdAndParentId(item.getId(), id));
        });
        if(menus.size() > 0 || childrens.size() > 0) {
            return JsonResult.fail("删除失败,该角色下拥有菜单信息,请删除菜单信息");
        }
        if (permissionMapper.findByRoleId(id).size() > 0) {
            return JsonResult.fail("删除失败,该角色下拥有权限信息,请先删除对应的权限信息");
        }
        roleMapper.deleteRole(id);
        return JsonResult.ok("删除菜单信息成功");
    }

    /**
     *  修改菜单信息
     * @param role 菜单信息
     * @return
     */
    @Transactional
    @Override
    public JsonResult<Object> updateRole(SysRole role) {
        roleMapper.updateRole(role);
        if (role.getPermissions().size() > 0) {
            permissionMapper.deleteByRoleId(role.getId());
            role.getPermissions().forEach(item -> {
                roleMapper.insertPermission(role.getId(),item.getId());
            });
        }
        if (role.getMenus().size() > 0) {
            menuMapper.deleteByRoleId(role.getId());
            role.getMenus().forEach(item -> roleMapper.insertMenus(role.getId(),item.getId()));
        }
        return JsonResult.ok("修改角色信息成功");
    }

    /**
     * 查询所有用户信息
     * @return 用户信息
     */
    @Override
    public JsonResult<Object> findAllRoles() {
        return JsonResult.ok(roleMapper.findAllRoles());
    }
}
